package minformer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.microedition.rms.RecordStore;


public class Options
{
	private static int VERSION = 2;
	private static String RMS_NAME = "weatheropt";
	
	public static final int VIEW_MODE_      = 10000;
	public static final int VIEW_MODE_FULL  = 10000;
	public static final int VIEW_MODE_SHORT = 10001;
	
	public static String backGrndImage;
	public static int cityNum;
	public static boolean textBorder;
	public static boolean onlyOneDay;
	public static int textColor;
	public static int backColor;
	public static int yPos;
	public static int requestInterval;
	public static int viewMode;
	public static int maxTemp;
	public static int minTemp;
	public static boolean useMaxTemp;
	public static boolean useMinTemp;
	
	static
	{
		setDefaults();
	}
	
	public static void setDefaults()
	{
		backGrndImage = null;
		cityNum = 27612;
		textBorder = true;
		textColor = 0x000000;
		backColor = 0xC0C0C0;
		yPos = 4;
		requestInterval = 6;
		viewMode = VIEW_MODE_FULL;
		maxTemp = 32;
		minTemp = 0;
		useMaxTemp = false;
		useMinTemp = false;
		onlyOneDay = false;
	}
	
	public static void save()
	{
		try
		{
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DataOutputStream dos = new DataOutputStream(baos);
			
			dos.writeInt(VERSION);
			dos.writeUTF(backGrndImage == null ? "" : backGrndImage);
			dos.writeInt(cityNum);
			dos.writeInt(textColor);
			dos.writeInt(backColor);
			dos.writeInt(yPos);
			dos.writeInt(requestInterval);
			dos.writeInt(viewMode);
			dos.writeInt(maxTemp);
			dos.writeInt(minTemp);
			dos.writeBoolean(useMaxTemp);
			dos.writeBoolean(useMinTemp);
			dos.writeBoolean(onlyOneDay);
			dos.writeBoolean(textBorder);

			RecordStore rs = RecordStore.openRecordStore(RMS_NAME, true);
			while (rs.getNumRecords() < 2) rs.addRecord(null, 0, 0);
			dos.flush();
			byte[] buf = baos.toByteArray();
			rs.setRecord(1, buf, 0, buf.length);

			dos.close();
			baos.close();
			rs.closeRecordStore();
		}
		catch (Exception e) 
		{
		}
	}
	
	public static void load()
	{
		setDefaults();
		try
		{
			RecordStore account = RecordStore.openRecordStore(RMS_NAME, false);
			byte[] buf = account.getRecord(1);
			account.closeRecordStore();
			
			ByteArrayInputStream bais = new ByteArrayInputStream(buf);
			DataInputStream dis = new DataInputStream(bais);
			
			int vers = dis.readInt();
			if (vers == VERSION)
			{
				backGrndImage   = dis.readUTF();
				cityNum         = dis.readInt();
				textColor       = dis.readInt();
				backColor       = dis.readInt();
				yPos            = dis.readInt();
				requestInterval = dis.readInt();
				viewMode        = dis.readInt();
				maxTemp         = dis.readInt();
				minTemp         = dis.readInt();
				useMaxTemp      = dis.readBoolean();
				useMinTemp      = dis.readBoolean();
				onlyOneDay      = dis.readBoolean();
				textBorder      = dis.readBoolean();
			}
			else setDefaults();

			dis.close();
			bais.close();
		}
		catch (Exception e) 
		{
			setDefaults();
		}
	}

}
